# 和与乘积

def count_windows(n, arr):
    """前缀和/积 + 滑动窗口"""
    # 前缀和/积数组
    prefix_sum, prefix_mul = [0] * (n + 1), [1] * (n + 1)
    prefix_sum[1], prefix_mul[1] = arr[0], arr[0]
    for i in range(2, n + 1):
        prefix_sum[i] = prefix_sum[i - 1] + arr[i - 1]
        prefix_mul[i] = prefix_mul[i - 1] * arr[i - 1]
    cnt = 0

    # 滑动窗口
    for l in range(1, n + 1):  # 开始的索引
        for win in range(n - l + 1):  # 遍历所有窗口的大小
            r = l + win
            s = prefix_sum[r] - prefix_sum[l - 1]  # 窗口元素之和
            m = prefix_mul[r] // prefix_mul[l - 1]  # 窗口元素的乘积
            if s == m:
                cnt += 1
    return cnt


def count_windows_opt(n, arr):
    arr = [0] + arr
    sum_arr = [0 for _ in range(n + 1)]  # 记录压缩后的区间和（包含1的贡献）
    num = [0 for _ in range(n + 1)]  # 存储非1元素的值
    onecnt = [0 for _ in range(n + 2)]  # 记录每个非1元素前后的1的数量
    cnt = 1

    for i in range(1, n + 1):
        if arr[i] == 1:
            onecnt[cnt] += 1  # 记录每个非1的数前面的1的个数
            sum_arr[cnt] += 1  # 累计当前非1元素前的1
        else:
            sum_arr[cnt] += sum_arr[cnt - 1] + arr[i]  # 累加总和的中间结果
            num[cnt] = arr[i]
            cnt += 1
    max_val = sum_arr[cnt - 1] + onecnt[cnt]  # 最大可能的和（所有1+非1元素的和）

    ans = n  # 单元素区间一定满足条件

    for i in range(1, cnt):
        p = num[i]
        for j in range(i + 1, cnt):
            p *= num[j]
            if p > max_val:  # 乘积超过最大可能和，直接终止
                break
            s = sum_arr[j] - sum_arr[i - 1] - onecnt[i]  # 计算非1元素区间的和（扣除左侧多余的1）
            d = p - s  # 计算差值
            if d == 0:
                ans += 1  # 直接匹配
            elif 0 < d <= onecnt[i] + onecnt[j + 1]:
                left = min(d, onecnt[i])  # 左侧最多能贡献的1
                right = min(d, onecnt[j + 1])  # 右侧最多能贡献的1
                ans += left + right - d + 1  # 组合数学计算有效区间数
    return ans


if __name__ == '__main__':
    # print(count_windows(4, [1, 3, 2, 2]))
    with open("../../data/2021_h.in", "r") as file:
        n = int(file.readline())
        arr = list(map(int, file.readline().split()))
    print(count_windows_opt(n, arr))  # 17026
